package com.ele.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.ele.pojo.Admin;
import com.ele.pojo.Shop;
import com.ele.utils.JDBCUtils;

/**
 * 	与数据之间的处理
 * @author candy
 *
 */
public class AdminDao {
	/**
	 * 	确认管理登入数据库
	 * @param admin	用户名
	 * @param password	密码
	 * @return	返回管理员信息对象
	 * @throws SQLException
	 */
	public Admin adminLogin(String admin, String password) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from admin where admin = ? and pwd = ?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, admin);
		ptmt.setString(2, password);
		ResultSet rs = ptmt.executeQuery();
		if (rs.next()) {
			Admin adm = new Admin();
			adm.setAdmin(rs.getString("admin"));
			adm.setPassword(rs.getString("pwd"));
			adm.setId(rs.getInt("id"));
			JDBCUtils.close(conn, ptmt, rs);
			return adm;
		}
		return null;
	}
	/**
	 * 	管理员添加商铺
	 * @param shop	存放的商铺信息对象
	 * @throws SQLException
	 */
	public void addShop(Shop shop) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "insert into shop values(null,?,?,?,?,?,?,?,?)";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1,shop.getShopName());
		ptmt.setString(2,shop.getAddr());
		ptmt.setString(3,shop.getShopDesc());
		ptmt.setDouble(4,shop.getStartPrice());
		ptmt.setDouble(5,shop.getFee());
		ptmt.setTimestamp(6,new Timestamp(shop.getRegistTime().getTime()));
		ptmt.setInt(7,shop.getShopStatus());
		ptmt.setString(8,shop.getPassword());
		
		ptmt.executeUpdate();
	}
	/**
	 * 	查询数据库所有商铺信息
	 * @return	返回一个存放所有信息的Arraylist集合
	 * @throws SQLException
	 */
	public List<Shop> selectShop() throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from shop";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ResultSet rs = ptmt.executeQuery();
		List<Shop> list = new ArrayList<>();
		while (rs.next()) {
			Shop shop = new Shop();
			shop.setId(rs.getInt("id"));
			shop.setShopName(rs.getString("shopName"));  
			shop.setAddr(rs.getString("shopAddr"));      
			shop.setShopDesc(rs.getString("shopdesc"));  
			shop.setStartPrice(rs.getDouble("startPrice"));
			shop.setFee(rs.getDouble("fee"));       
			shop.setRegistTime(rs.getDate("registTime"));
			shop.setShopStatus(rs.getInt("shopStatus"));   
			shop.setPassword(rs.getString("shoppwd"));
			list.add(shop);
		}
		JDBCUtils.close(conn, ptmt,rs);
		return list;
	}
	/**
	 * 	以商铺编号查询商铺信息
	 * @param id	商铺编号
	 * @return	商铺信息
	 * @throws SQLException
	 */
	public Shop selectShop(Integer id) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from shop where id = ?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setInt(1, id);
		ResultSet rs = ptmt.executeQuery();
		Shop shop = new Shop();
		if(rs.next()) {
			shop.setId(rs.getInt("id"));
			shop.setShopName(rs.getString("shopName"));  
			shop.setAddr(rs.getString("shopAddr"));      
			shop.setShopDesc(rs.getString("shopdesc"));  
			shop.setStartPrice(rs.getDouble("startPrice"));
			shop.setFee(rs.getDouble("fee"));       
			shop.setRegistTime(rs.getDate("registTime"));
			shop.setShopStatus(rs.getInt("shopStatus"));   
			shop.setPassword(rs.getString("shoppwd"));
			
			JDBCUtils.close(conn, ptmt,rs);
			return shop;
		}
		return null;
	}
	/**
	 * 	更新指定编号的商铺信息
	 * @param shop	商铺信息
	 * @return	返回一个boolean类型数据，true为更新修改成功，false反之
	 * @throws SQLException
	 */
	public Boolean updateShop(Shop shop) throws SQLException {
		Connection conn = JDBCUtils.getConnection();
		String sql = "update shop set shopName=?,"
				+ "shopAddr=?,shopdesc=?,startPrice=?,"
				+ "fee=?,shopStatus=?,shoppwd=? where id =?";
		PreparedStatement ptmt = conn.prepareStatement(sql);
		ptmt.setString(1, shop.getShopName());
		ptmt.setString(2, shop.getAddr());
		ptmt.setString(3, shop.getShopDesc());
		ptmt.setDouble(4, shop.getStartPrice());
		ptmt.setDouble(5, shop.getFee());
		ptmt.setInt(6, shop.getShopStatus());
		ptmt.setString(7, shop.getPassword());
		ptmt.setInt(8, shop.getId());
		
		int x = ptmt.executeUpdate();
		if (x>0) {
			return true;
		}else {
			return false;
		}
		
	}
}
